layui.define([ 'form', 'laydate', 'table' ], function(exports) {
	var form = layui.form;
	var table = layui.table;
    var index = layui.index;
    
    var view = {
        init:function(){
        	this.aliyunVerifyCode();
        	this.initSubmit();
        	 
        },
       
        aliyunVerifyCode:function(){ //阿里云验证码验证
        	 var wait=60;
             var state = true;

             function time() {
                 if (wait == 0) {
                     state = true;
                     $('.huoqu_vcode').text("免费获取");
                     wait = 60;
                     $('.huoqu_vcode').attr("disabled", false);
                 } else {
                     $('.huoqu_vcode').attr("disabled", true);
                     $('.huoqu_vcode').text(wait + "秒后可重发");
                     wait--;
                     setTimeout(function() {
                         time()
                     },1000)
                 }
             }

             
             $(document).on('click','.huoqu_vcode',function(){
            	 if(!aliVerifyStatus){
                	 layer.alert("请点击验证按钮", {icon: 2});
                	 return false;
                 }
            	 
                 if($(this).attr("disabled")){
                     return;
                 }
                 time();
                 var mobile = $('.phone_input').val();
                              
                 var verifyCodeData={};
                 verifyCodeData['aliSessionId']=aliSessionId;
                 verifyCodeData['aliToken']=aliToken;
                 verifyCodeData['aliSig']=aliSig;
                 verifyCodeData['aliScene']=aliScene;
                 verifyCodeData['mobile']=mobile;
               
                 $.ajax({
                		 type: "POST",
                		 url:"/user/popUserSecurity/getVerifyCode.json",
                		 data:{"verifyCodeData":JSON.stringify(verifyCodeData)},
                		 headers:{
                		        "ip":returnCitySN["cip"]
                		    },
            		    success: function(data){
        	       			if(data.code == '1'){
        	                       if(state){
        	                           time();
        	                       }
        	                       state = false;
        	                   }else{
        	                  	 layer.alert(data.message, {icon: 2});
        	                   }
        	              	
        	                   window.ic.reset();
        	                   aliVerifyStatus=false;
        	       		  }               		 
                         });                               
             });

        },
        
       initSubmit:function(){
         $('.area_password').hide();
         show_step(0);//下一步导引显示
         function show_step(index){

             $('.area_password').hide();
             $($('.area_password').get(index)).show();

             $('.p_rod > span').removeClass('active');
             $($('.p_rod > span').get(index)).addClass('active');

         }
         
         
         //第一步
         $(document).on('click','.step_1',function(){        	 
             //验证手机号格式
        	 var phone = $(".phone_input").val();
        	 var loginName =$(".login_input").val();
             if(!(/^1[34578]\d{9}$/.test(phone))){
            	 layer.alert('请输入11位有效的手机号', {icon: 2});
            	 return false;
             }
             
             show_step(1);
             $($(".phone_input").get(1)).val(phone);
             $($(".login_input").get(1)).val(loginName);

         });
         
         //第二步,获取验证码
         $(document).on('click','.step_2',function(){
        	
        	 if($(".code_input").val().length==0){
        		 layer.alert('请输入验证码', {icon: 2});
        		 return false;
        	 }
        	 
        	 var mobile = $('.phone_input').val();
        	 var verifyCode = $(".code_input").val();
        	 $.post("/user/popUserSecurity/checkVerifyCode.json",{"mobile":mobile,"verifyCode":verifyCode},vcode);
             
             function vcode(data){//获取验证码的请求
            	 //console.log(data.code);
            	 if(data.code == '1'){
            		 show_step(2);
                 }else{
                	 layer.alert(data.message, {icon: 2});
                 }
        	 
             }
             

         });

         //第三步
         $(document).on('click','.step_3',function(){
   
             var mobile= $(".phone_input").val();
             var newPassword = $(".password_input").val();
             var loginName =$(".login_input").val();
             if(newPassword != $(".note_password_input").val()){
            	 layer.alert('两次密码输入不一致', {icon: 2});
                 return;
             }

             doAjax('/user/popUserSecurity/setAdvancePass.json','post',{"mobile":mobile,"newPassword":newPassword,"loginName":loginName},function(res){
            	 if(res.code == '1'){
            		 show_step(3);
                 }else{
                	 layer.alert(res.message, {icon: 2});
                 }
            	 
            });                  
         });
         
         //第四步
         $(document).on('click','.step_4',function(){
        	 	window.location.href="/user/popUserSecurity/index.do";
         }); 
        
       }
        }

    exports('setAdvancePass',view);
});